package com.viviet.coreservices.ewallet;

import com.viviet.mobileservices.business.impl.CustomerServiceImpl;
import com.viviet.mobileservices.common.Constants;
import com.viviet.mobileservices.domain.info.MessageInfo;
import com.viviet.mobileservices.util.Logit;

public class EWSConnector {
	private static Logit LOG = Logit.getInstance(CustomerServiceImpl.class);
	private final String INVALID_REQUEST = "04";
	private static final String OPERATION_SUCCESS = "00";
	private final String INACTIVE_CHANNEL = "03";
	private final String WRONG_PASSWORD = "01";
	private final String NOT_FOUND_CHANNEL = "02";
	private final String OTHER_ERRORS = "99";
	private static final String SYSTEM_SENDER = "IBSYS";
	
	/**
	 * 
	 * @param customerId
	 * @return Balance
	 */
	public static MessageInfo getBalance(String customerId) {
		String messageXML = "<input><service>BLIQ</service></input>";
		EWalletWSCaller ws = new EWalletWSCaller();
		MessageInfo msg = null;
		try {
			msg = parserRetValue(ws.callService(customerId, messageXML));
		} catch (Exception e) {
			msg = new MessageInfo();
			msg.setMsgType(1);
			msg.setMsgType(Constants.OPERATION_ERROR);
			LOG.error(e);
		}
		return msg;
	}
	
	public static MessageInfo transferMoney (String sender,String debitAcc,String creditAcc, Integer amount, String comment) {
		LOG.debug("Transfer money: ");
		String messageXML = "<input>    <service>CT</service>    <funds_transfer>        <debit>%s</debit>        <credit>%s</credit>        <amount>%s</amount>        <description>%s</description>        <charge></charge>    </funds_transfer></input>";
		messageXML = messageXML.format(messageXML, debitAcc,creditAcc,amount,comment);
		LOG.debug("Input messageXML: "+messageXML);
		EWalletWSCaller ws = new EWalletWSCaller();
		MessageInfo msg = null;
		try {
			msg = parserRetValue(ws.callService(sender, messageXML));
		} catch (Exception e) {
			msg = new MessageInfo();
			msg.setMsgType(Constants.OPERATION_ERROR);
			msg.setMsgCode(Constants.TRAN_FAILED);
			LOG.error(e);
		}
		
		return msg;
	}
	
	public static MessageInfo verifyPassword(String customerId, String password) {
		LOG.debug("Verify password: ");
		String messageXML = "<input> <service>VerifyPassword</service> <verify_password> <login_id> %s </login_id> <login_pass> %s </login_pass> </verify_password> </input>";
		messageXML = messageXML.format(messageXML, customerId, password);
		LOG.debug("Input messageXML: "+messageXML);
		EWalletWSCaller ws = new EWalletWSCaller();
		MessageInfo msg = null;
		try {
			msg = parserRetValue(ws.callService(SYSTEM_SENDER, messageXML));
		} catch (Exception e) {
			msg = new MessageInfo();
			msg.setMsgType(Constants.OPERATION_ERROR);
			LOG.error(e);
		}
		
		return msg;
	}
	
	/**
	 * Parse String returning by Core Services
	 * @param retValue
	 * @return
	 */
	private static MessageInfo parserRetValue(String retValue) {
		String[] retValues = retValue.split("\\|");
		String[] msgCode = retValues[1].split("\\@");
		MessageInfo msg = new MessageInfo();
		LOG.info("### Return code: "+retValues[0]);
		if (OPERATION_SUCCESS.equals(retValues[0])) {
			msg.setMsgType(Constants.OPERATION_SUCCESS);
			LOG.info("### Operate successful");	
			//msg.setMsgContent(msgContent)
		} else {
			LOG.info("### Operate error");
			msg.setMsgType(Constants.OPERATION_ERROR);
			LOG.info("### Detail error code: "+msgCode[0]+ " --- "+msgCode[1]);
		}
		
		msg.setMsgCode(msgCode[0]);
		msg.setMsgContent(msgCode[1]);
		
		return msg;
	}
	
	/**
	 * Change password for user
	 * @param sender
	 * @param oldPassword
	 * @param newPassword
	 * @return
	 */
	public static MessageInfo changePassword(String sender,String oldPassword, String newPassword) {
		LOG.debug("Change password: ");
		String messageXML = "<input> <service>MK</service> <change_password> <old_pass> %s </old_pass> <new_pass> %s </new_pass> </change_password> </input>";
		messageXML = messageXML.format(messageXML, oldPassword, newPassword);
		
		MessageInfo msg = null;
		EWalletWSCaller ws = new EWalletWSCaller();
		
		try {
			msg = parserRetValue(ws.callService(sender, messageXML));
		} catch (Exception e) {
			msg = new MessageInfo();
			msg.setMsgType(1);
			msg.setMsgType(Constants.OPERATION_ERROR);
			LOG.error(e);
		}
		
		return msg;
	}
}
